Return type | Name and parameters |
---|---|
boolean
|
any(Closure predicate)
Iterates over the contents of a char Array, and checks whether a predicate is valid for at least one element. |
boolean
|
asBoolean()
Coerces a char array to a boolean value. |
List
|
chop(int chopSizes)
Chops the char array into pieces, returning lists with sizes corresponding to the supplied chop sizes. |
boolean
|
contains(Object value)
Checks whether the array contains the given value. |
Number
|
count(Object value)
Counts the number of occurrences of the given value inside this array. |
char[]
|
each(Consumer consumer)
Iterates through a char[] passing each char to the given consumer. |
char[]
|
eachWithIndex(Closure closure)
Iterates through a char[], passing each char and the element's index (a counter starting at zero) to the given closure. |
boolean
|
equals(char[] right)
Compares the contents of this array to the contents of the given array. |
boolean
|
every(Closure predicate)
Iterates over the contents of a char Array, and checks whether a predicate is valid for all elements. |
char
|
first()
Returns the first item from the char array. |
List
|
flatten()
Flattens an array. |
List
|
getAt(IntRange range)
Supports the subscript operator for a char array with an IntRange giving the desired indices. |
List
|
getAt(ObjectRange range)
Supports the subscript operator for a boolean array with an ObjectRange giving the desired indices. |
List
|
getAt(Range range)
Supports the subscript operator for a char array with a range giving the desired indices. |
List
|
getAt(Collection indices)
Supports the subscript operator for a char array with a (potentially nested) collection giving the desired indices. |
IntRange
|
getIndices()
Returns indices of the char array. |
char
|
head()
Returns the first item from the char array. |
char[]
|
init()
Returns the items from the char array excluding the last item. |
String
|
join()
Concatenates the string representation of each item in this array. |
String
|
join(String separator)
Concatenates the string representation of each item in this array, with the given String as a separator between each item. |
char
|
last()
Returns the last item from the char array. |
char[]
|
reverse()
Creates a new char array containing items which are the same as this array but in reverse order. |
char[]
|
reverse(boolean mutate)
Reverses the items in an array. |
char[]
|
reverseEach(Closure closure)
Iterates through a char[] in reverse order passing each char to the given closure. |
int
|
size()
Provides arrays with a size method similar to collections.
|
Stream
|
stream()
Returns a sequential Stream with the specified array as its source. |
char
|
sum()
Sums the items in an array. |
char
|
sum(char initialValue)
Sums the items in an array, adding the result to some initial value. |
char[]
|
swap(int i, int j)
Swaps two elements at the specified positions. |
char[]
|
tail()
Returns the items from the char array excluding the first item. |
List
|
toList()
Converts this array to a List of the same size, with each element added to the list. |
Set
|
toSet()
Converts this array to a Set, with each unique element added to the set. |
String
|
toString()
Returns the string representation of the given array. |
Iterates over the contents of a char Array, and checks whether a predicate is valid for at least one element.
char[] array = ['a', 'b', 'c'] assert array.any{ it <= 'a' } assert !array.any{ it < 'a' }
predicate
- the closure predicate used for matchingCoerces a char array to a boolean value. A char array is false if the array is of length 0, and true otherwise.
Chops the char array into pieces, returning lists with sizes corresponding to the supplied chop sizes. If the array isn't large enough, truncated (possibly empty) pieces are returned. Using a chop size of -1 will cause that piece to contain all remaining items from the array.
char[] array = [0, 1, 2] assert array.chop(1, 2) == [[0], [1, 2]]
chopSizes
- the sizes for the returned piecesChecks whether the array contains the given value.
value
- the value being searched forCounts the number of occurrences of the given value inside this array.
Comparison is done using Groovy's == operator (using
compareTo(value) == 0
).
char[] array = ['x', 'y', 'z', 'z', 'y'] assert array.count('y') == 2
value
- the value being searched forIterates through a char[] passing each char to the given consumer.
char[] array = ['a' as char, 'b' as char, 'c' as char] String result = '' array.each{ result += it } assert result == 'abc'
consumer
- the consumer for each charIterates through a char[], passing each char and the element's index (a counter starting at zero) to the given closure.
char[] array = ['a' as char, 'b' as char, 'c' as char]
String result = ''
array.eachWithIndex{ item, index ->
result += "$index($item)" }
assert result == '0(a)1(b)2(c)'
closure
- a Closure to operate on each charCompares the contents of this array to the contents of the given array.
Example usage:
char[] array1 = ['a', 'b'] char[] array2 = ['a', 'b'] assert array1 !== array2 assert array1.equals(array2)
right
- the array being comparedIterates over the contents of a char Array, and checks whether a predicate is valid for all elements.
char[] array = ['a', 'b', 'c'] assert array.every{ it <= 'd' } assert !array.every{ it > 'b' }
predicate
- the closure predicate used for matchingReturns the first item from the char array.
char[] chars = ['a', 'b', 'c'] assert chars.first() == 'a'An alias for
head()
.
Flattens an array. This array is added to a new collection.
It is an alias for toList()
but allows algorithms to be written which also
work on multidimensional arrays or non-arrays where flattening would be applicable.
char[] array = 'ab'.chars assert array.flatten() == ['a', 'b']
Supports the subscript operator for a char array with an IntRange giving the desired indices.
char[] array = 'abcdef'.chars assert array[2..3] == ['c', 'd'] assert array[-2..-1] == ['e', 'f'] assert array[-1..-2] == ['f', 'e']
range
- an IntRange indicating the indices for the items to retrieveSupports the subscript operator for a boolean array with an ObjectRange giving the desired indices.
char[] array = 'abcdef'.chars def range = new ObjectRange(2, 3) assert array[range] == ['c', 'd']
range
- an ObjectRange indicating the indices for the items to retrieveSupports the subscript operator for a char array with a range giving the desired indices.
char[] array = 'abcdef'.chars assert array[2..<2] == [] // EmptyRange assert array[(0..5.5).step(2)] == ['a', 'c', 'e'] // NumberRange assert array[(1..5.5).step(2)] == ['b', 'd', 'f'] // NumberRange
range
- a range indicating the indices for the items to retrieveSupports the subscript operator for a char array with a (potentially nested) collection giving the desired indices.
char[] array = 'abcde'.chars assert array[2, 3] == ['c', 'd'] assert array[1, 0..1, [0, [-1]]] == ['b', 'a', 'b', 'a', 'e']
indices
- a collection of indices for the items to retrieveReturns indices of the char array.
char[] array = 'ab'.chars assert array.indices == 0..1
Returns the first item from the char array.
char[] chars = ['a', 'b', 'c'] assert chars.head() == 'a'An alias for
first()
.
Returns the items from the char array excluding the last item.
char[] chars = ['a', 'b', 'c'] def result = chars.init() assert result == ['a', 'b'] assert chars.class.componentType == result.class.componentType
Concatenates the string representation of each item in this array.
Concatenates the string representation of each item in this array, with the given String as a separator between each item.
separator
- a String separatorReturns the last item from the char array.
char[] chars = ['a', 'b', 'c'] assert chars.last() == 'c'
Creates a new char array containing items which are the same as this array but in reverse order.
char[] array = ['a', 'b'] assert array.reverse() == ['b', 'a'] as char[]
Reverses the items in an array. If mutate is true, the original array is modified in place and returned. Otherwise, a new array containing the reversed items is produced.
char[] array = ['a', 'b', 'c'] def yarra = array.reverse(true) assert array == ['c', 'b', 'a'] assert yarra == ['c', 'b', 'a'] assert array === yarra yarra = array.reverse(false) assert array !== yarra assert array == ['c', 'b', 'a'] assert yarra == ['a', 'b', 'c']
mutate
- true
if the array itself should be reversed in place, false
if a new array should be createdIterates through a char[] in reverse order passing each char to the given closure.
char[] array = 'abc'.chars String result = '' array.reverseEach{ result += it } assert result == 'cba'
closure
- the closure applied on each charProvides arrays with a size
method similar to collections.
Returns a sequential Stream with the specified array as its source.
Stream
for the arraySums the items in an array.
assert (1+2+3+4 as char) == ([1,2,3,4] as char[]).sum()
Sums the items in an array, adding the result to some initial value.
assert (5+1+2+3+4 as char) == ([1,2,3,4] as char[]).sum(5 as char)
initialValue
- the items in the array will be summed to this initial valueSwaps two elements at the specified positions.
Example:
assert ([1, 3, 2, 4] as char[]) == ([1, 2, 3, 4] as char[]).swap(1, 2)
i
- a positionj
- a positionReturns the items from the char array excluding the first item.
char[] chars = ['a', 'b', 'c'] def result = chars.tail() assert result == ['b', 'c'] assert chars.class.componentType == result.class.componentType
Converts this array to a List of the same size, with each element added to the list.
Converts this array to a Set, with each unique element added to the set.
char[] array = 'xyzzy'.chars Set expected = ['x', 'y', 'z'] assert array.toSet() == expected
Returns the string representation of the given array.
char[] array = 'abcd'.chars assert array instanceof char[] assert array.toString() == 'abcd'